import MyStore from '@/store/index.js'
import config from "@/config"; // 导入配置文件

// http://47.115.211.73:8080/district/adcode/520111

// 基本路径
const baseURL = config.baseURL;


export function submitRepairInformation(data) {
	console.log("进入提交维修信息请求");
	console.log("提交维修信息:", data);
	const token = uni.getStorageSync('token');
	uni.request({
		url: `${baseURL}/order/insert`,
		method: "POST",
		data,
		header: {
			"token": token
		},
		success(res) {
			console.log("提交维修信息返回的数据", res);
			if (res.data.code == 200) {
				uni.showToast({
					duration: 2000,
					title: "报修成功",
					image: '/static/image/icon/success.png',
				})

				setTimeout(function() {
					uni.reLaunch({
						url: '/pages/index/index'
					})
				}, 2000)


			} else {
				uni.showToast({
					duration: 2000,
					title: "报修失败",
					image: '/static/image/icon/fail.png',
				})
			}
		},
		fail(err) {
			console.log("服务器罢工了");
		},
	});
}


// 获取所有故障类型
export function getAllFaultTypes() {
    return new Promise((resolve, reject) => {
		const token = uni.getStorageSync('token');
        uni.request({
            url: `${baseURL}/faultType/1`,
            method: 'GET',
            header: {
                "token": token
            },
            success(res) {
                if (res.data.code === 200) {
                    const faultTypes = res.data.data.records.map(item => item.name);
                    resolve(faultTypes);
                } else {
                    reject("获取数据失败");
                }
            },
            fail(err) {
                reject("服务器罢工了");
            }
        });
    });
}

// 获取所有设备型号
export function getAllMachineModels() {
    return new Promise((resolve, reject) => {
		const token = uni.getStorageSync('token');
        uni.request({
            url: `${baseURL}/userMachine/list`,
            method: 'GET',
            header: {
                "token": token
            },
            data: {},
            success(res) {
                if (res.data.code === 200) {
                    const machineModels = res.data.data.map(item => item.modelNumber);
                    resolve(machineModels);
                } else {
                    reject("请求失败");
                }
            },
            fail(err) {
                reject("服务器罢工了");
            }
        });
    });
}




